    //
//  FixturesController.m
//  NBANews
//
//  Created by Liccon Chang on 11-1-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FixturesController.h"
#import "GamblingCompany.h"
#import "ColumnView.h"
#import "GameInfo.h"
#import "GameInfoCell.h"
#import "DetailTabController.h"

@implementation FixturesController

@synthesize iDatePicker;
@synthesize GamblingCompanyView;
@synthesize mainTable;
@synthesize iRacesCard;
@synthesize iGamblingCompanys;

-init{
	self = [super init];
	if (self) {
		self.title = @"赛程";
		
		iGamblingCompanys = [[NSMutableArray alloc] init];
		GamblingCompany* company = [[GamblingCompany alloc] init];
		company.Name = @"澳门";
		[iGamblingCompanys addObject:company];
		[company release];
		GamblingCompany* company2 = [[GamblingCompany alloc] init];
		company2.Name = @"皇马";
		[iGamblingCompanys addObject:company2];
		[company2 release];
		
		
		iRacesCard = [[RacesCard alloc] init];
		GameInfo* game = [[GameInfo alloc] init];
		game.Hometeam = @"火箭火箭火";
		game.Visitors = @"火箭火箭火";
		game.HometeamScore = @"98";
		game.VisitorsScore = @"100";
		game.State = @"game over";
		game.GameTime = @"9:00";
		[iRacesCard.iGameInfos addObject:game];
		[game release];
		
		GameInfo* game2 = [[GameInfo alloc] init];
		game2.Hometeam = @"rocket";
		game2.Visitors = @"laker";
		game2.HometeamScore = @"98";
		game2.VisitorsScore = @"100";
		game2.State = @"game over";
		game2.GameTime = @"19:00";
		[iRacesCard.iGameInfos addObject:game2];
		[game2 release];
	}
	return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	
	UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 40)];
	UIImage* topBg = [UIImage imageNamed:@"nav_bg.png"];
    UIColor *topbgc = [[UIColor alloc] initWithPatternImage:topBg];
	topView.backgroundColor = topbgc;
	
	iDatePicker = [[DatePickerView alloc] initWithFrame:CGRectMake(0, 1, 110, 38) showInControler:self onComplete:@selector(onCompleteSelectDate)];
	iDatePicker.Direction = 1;
	[topView addSubview:iDatePicker];

	GamblingCompanyView = [self CreateGamblingCompanyView:CGRectMake(110, 1, self.view.frame.size.height - 110, 38)];
	[topView addSubview:GamblingCompanyView];
	[self.view addSubview:topView];
	[topView release];
	
	CGRect rect;
	rect.size.width = self.view.frame.size.height;
	rect.origin.x = 0;
	rect.origin.y = 40;
	rect.size.height = self.view.frame.size.width - TabBarHeight - NarBarHeight - StateBarHeight - AdBarHeight;
	mainTable = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
	mainTable.dataSource = self;
	mainTable.delegate = self;
	mainTable.backgroundColor = [UIColor clearColor];
	[self.view addSubview:mainTable];
	
	[self InsertAdvertisementsView];
	[self addNavExit];
}

- (void)viewDidAppear:(BOOL)animated{
	if (iRacesCard == nil) {
		[self getRacesCard];
	}
}

-(UIView*) CreateGamblingCompanyView:(CGRect) frame{
	ColumnView* Gamblingview = [[ColumnView alloc] initWithFrame:frame];
	Gamblingview.backgroundColor = [UIColor clearColor];
	UIButton* btn = nil;
	GamblingCompany* company = nil;
	CGRect rect;
	CGSize stringSize;
	for (int i = 0; i < [iGamblingCompanys count]; i++) {
		company = [iGamblingCompanys objectAtIndex:i];
		rect = frame;
		rect.origin = CGPointMake(0, 0);
		
		btn = [UIButton buttonWithType:UIButtonTypeCustom];
		[btn setBackgroundColor:[UIColor clearColor]];
		[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
		btn.tag = i;
		[btn setTitle:company.Name forState:UIControlStateNormal];
		stringSize = [company.Name sizeWithFont:btn.titleLabel.font];
		rect.size.width = stringSize.width;
		btn.frame = rect;
		[btn addTarget:self action:@selector(GamblingCompanyAction:) forControlEvents:UIControlEventTouchUpInside];
		[Gamblingview addSubview:btn];
		if (i == 0) {
			[btn setSelected:YES];
		}
	}
	return Gamblingview;
}

- (void)GamblingCompanyAction:(id)sender{
	iCurrentCompany = [sender tag];
	for (UIView* btn in [GamblingCompanyView subviews]) {
		if([btn isKindOfClass:[UIButton class]])
			[(UIButton*)btn setSelected:NO];
	}
	[(UIButton*)sender setSelected:YES];
}

-(void) onCompleteSelectDate{
	if (iRacesCard && [iDatePicker.iShowDateLabel.text isEqualToString:iRacesCard.date]) {
		[self getRacesCard];
	}
}

-(void) getRacesCard{
	NSString* url = [NSString stringWithFormat:[Constant getAddress:ERacesCard],iDatePicker.iShowDateLabel.text];
	network = [self submitURL:url withOperation:@"getRacesCard"];
}

- (void)completeFetch:(NetworkUtil *)obj{
	[super completeFetch:obj];
	//prase RacesCard data
	iRacesCard = [RegexParse RegexParseRacesCard:[network getResponse]];
	[mainTable reloadData];
	[network release];
	network =nil;
}


/////////////////////////////////////////////////////////////////
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 80.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {
	if (iRacesCard && iRacesCard.iGameInfos) {
		return [iRacesCard.iGameInfos count];
	}
	return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView   cellForRowAtIndexPath:(NSIndexPath*)indexPath {
	
	static NSString *cellIndentifier = @"Cell";
	int row = [indexPath row];
	GameInfo* info;
	info = [iRacesCard.iGameInfos objectAtIndex:row];
	UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
	if(cell ==  nil) {
		cell = [[[GameInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier data:info] autorelease];
	}
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	int row = [indexPath row];
	GameInfo* info;
	info = [iRacesCard.iGameInfos objectAtIndex:row];

	DetailTabController* detail = [[DetailTabController alloc] initWithGameInfo:info];
	[self.navigationController pushViewController:detail animated:YES];
	[detail release];
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[iDatePicker release];
	[GamblingCompanyView release];
	[iRacesCard release];
	[iGamblingCompanys release];
    [super dealloc];
}


@end
